﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aras.IOM;

namespace ArasConnection
{
    public static class CUpLoadFile
    {
        /// <summary>
        /// 上传Part 的PDF图纸文件
        /// </summary>
        /// <param name="PItemNumber">Part 的货品品号</param>
        /// <param name="FileName">PDF文件名</param>
        /// <param name="FilePath">PDF文件路径</param>
        public static bool uploadFile(string PItemNumber,string FileName,string FilePath)
        {
            try
            {
                string AMLStr = "<AML><Item type=\"Part\" action=\"get\" where=\"[Part].item_number=\'" + PItemNumber + "\' \"></Item></AML>";
                Item pItem = PublicState.ArasLoginInnovator.applyAML(AMLStr);
                Item fItem = PublicState.ArasLoginInnovator.newItem("File", "add");
                fItem.setProperty("filename", FileName);
                fItem.attachPhysicalFile(FilePath+@"\"+FileName);
                fItem = fItem.apply();
                pItem.setAction("edit");
                pItem.setPropertyItem("pdfview", fItem);
                pItem = pItem.apply();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 上传Part 的工艺文件和检验指导书
        /// </summary>
        /// <param name="PItemNumber">Part货品品号</param>
        /// <param name="FileName">PDF文件名</param>
        /// <param name="FilePath">PDF路径</param>
        /// <param name="rPartItem">RelationShipItem Name</param>
        /// <param name="RePartItem">RelatedItem Name</param>
        /// <param name="tfzc">制程信息</param>
        public static bool uploadFile(string PItemNumber, string FileName, string FilePath,string rPartItem,string RePartItem,Common.CTFZC tfzc)
        {
            try
            {
                string AMLString = "<AML><Item type=\"Part\" action=\"get\" where=\"item_number=\'" + PItemNumber + "\' \"><Relationships><Item type=\"" + rPartItem + "\" action=\"get\"><related_id><Item type=\"" + RePartItem + "\" action=\"get\" where=\"zcno=\'" + FileName + "\' \"></Item></related_id></Item></Relationships></Item></AML>";

                Item fItem = PublicState.ArasLoginInnovator.newItem("File", "add");
                fItem.setProperty("filename", FileName);
                fItem.attachPhysicalFile(FilePath + @"\" + FileName);
                fItem = fItem.apply();

                Item partItem = PublicState.ArasLoginInnovator.applyAML(AMLString);
                if (!partItem.isEmpty() && !partItem.isError())
                {
                    //PLM中存在相关信息，更新信息
                    Item partRelatedItem = partItem.getRelatedItem();
                    partRelatedItem.setAction("edit");
                    partRelatedItem.setProperty("zcno", tfzc.zcno);
                    partRelatedItem.setProperty("zcname", tfzc.zcname);
                    partRelatedItem.setProperty("zcrem", tfzc.zcrem);
                    partRelatedItem.setPropertyItem("pdf", fItem);

                    partRelatedItem = partRelatedItem.apply();
                }
                else
                {
                    Item pItem = PublicState.ArasLoginInnovator.newItem(RePartItem);
                    pItem.setProperty("zcno", tfzc.zcno);
                    pItem.setProperty("zcname", tfzc.zcname);
                    pItem.setProperty("zcrem", tfzc.zcrem);
                    pItem.setPropertyItem("pdf", fItem);
                    pItem = pItem.apply();

                    Item rItem = PublicState.ArasLoginInnovator.newItem(rPartItem);
                    rItem.setRelatedItem(pItem);
                    rItem = rItem.apply();

                    string AMLStr = "<AML><Item type=\"Part\" action=\"get\" where=\"[Part].item_number=\'" + PItemNumber + "\' \"></Item></AML>";
                    Item parentItem = PublicState.ArasLoginInnovator.applyAML(AMLStr);

                    parentItem.setAction("edit");
                    parentItem.addRelationship(rItem);
                    parentItem.apply();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
